<?php

/**
 * article actions.
 *
 * @package    jiazhang
 * @subpackage article
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class articleActions extends sfActions
{

  //发布文章评论,此处不进行ID关联验证，将在输出数据时判断并抛弃无效关联
  public function executeCreate_comment(sfWebRequest $request){
      sfConfig::set('sf_web_debug',false);
      if($request->isMethod('POST')){
          $user_id = (int)$request->getParameter('user_id',0);
          $article_id = (int)$request->getParameter('article_id',0);
          $content = trim($request->getParameter('content',''));
          $article_commend = new ArticleComment();
          $article_commend->setUserId($user_id)->setArticleId($article_id)->setContent($content)->save();
          $msg = array('error'=>0,'msg'=>'评论保存成功！');
          return $this->renderText(json_encode($msg));
      }
  }    
    
  public function executeList(sfWebRequest $request)
  {     
        sfConfig::set('sf_web_debug',false);
        $page_size = (int)$request->getParameter('page_size',11);
        $dql = Doctrine::getTable('Article')->createQuery()->where('is_commend = 0')->andWhere('display = 1')->orderBy('created_at desc');
        $page = (int)$request->getParameter('page',1);
        $articles = array();
        $i=0;
        
        if($page == 1){
            $page_size = $page_size - 4;
            $recommend_article = Doctrine::getTable('Article')->createQuery()->where('is_commend = 1')->orderBy('id desc')->limit(0,4)->execute();
            if($recommend_article->count() > 0){
                foreach($recommend_article as $article){
                    $articles[$i]['id'] = $article->getId();
                    $articles[$i]['title'] = $article->getTitle();
                    $articles[$i]['detail'] = $article->getSummary();
                    $articles[$i]['is_recommend'] = $article->getIsCommend();
                    $recommend = Doctrine::getTable('RecommendArticle')->findOneByArticleId($article->getId());
                    $articles[$i]['picture'] = $recommend->getCover();
                    $articles[$i]['commentNum'] = $article->getCommendCount();
                    $i++;
                }
            }
        }
        
        $this->pager = new sfDoctrinePager('Article',$page_size);
        $this->pager->setQuery($dql);
        $this->pager->setPage($page);
        $this->pager->init();
        
        foreach($this->pager->getResults() as $article){
            $articles[$i]['id'] = $article->getId();
            $articles[$i]['title'] = $article->getTitle();
            $articles[$i]['detail'] = $article->getSummary();
            $articles[$i]['is_recommend'] = $article->getIsCommend();
            $articles[$i]['picture'] = $article->getCover();
            $articles[$i]['commentNum'] = $article->getCommendCount();
            $i++;
        }
        
        return $this->renderText(json_encode($articles));
  }
  
  public function executeGet_info(sfWebRequest $request){
      $id = (int)$request->getParameter('id',0);
      $article = Doctrine::getTable('Article')->findOneById($id);
      $return_article = array();
     
      
      if($article){
          $article->incrementClick();
          $return_article['person'] = array();
          $return_article['commentList'] = array();
          $return_article['id'] = $article->getId();
          $return_article['title'] = $article->getTitle();
          $return_article['detail'] =  $article->getPhoneContent();
          $return_article['createTime'] = strtotime($article->getCreatedAt());
          $return_article['lookNum'] = $article->getArticleAttr()->getClick();
          $return_article['commentNum'] = $article->getCommendCount();
          $return_article['person']['name'] = $article->getAuthor();
          $comments = Doctrine::getTable('ArticleComment')->createQuery()->where('display = ?',1)->andWhere('article_id = ?',$article->getId())->orderBy('created_at desc')->execute();
          $i=0;
          
          foreach($comments as $comment){
              $return_article['commentList'][$i]['detail'] = $comment->getContent();
              $return_article['commentList'][$i]['person'] = array();
              if(!$comment->getUser())                  continue;
              $return_article['commentList'][$i]['person']['id']  = $comment->getUser()->getId();
              $return_article['commentList'][$i]['person']['name'] = $comment->getUser()->getNickname();
              $return_article['commentList'][$i]['person']['icon'] = $comment->getUser()->getCover();
              $return_article['commentList'][$i]['person']['familyRole'] = User::$roles[$comment->getUser()->getRole()];
              $return_article['commentList'][$i]['person']['children'] = array();
              
              $childs = $comment->getUser()->getChilds();
                      
              if(count($childs) > 0){
                  $j = 0;
                  foreach($childs as $child){
                      $return_article['commentList'][$i]['person']['children'][$j]['name'] = $child->getName();
                      $return_article['commentList'][$i]['person']['children'][$j]['age'] = $child->getChildOld();
                      $return_article['commentList'][$i]['person']['children'][$j]['sex'] = $child->getSexToString();
                      $j++;
                  }
              }
              
              $i++;
          }
      }
      
      return $this->renderText(json_encode($return_article));
  }
}
